$(document).ready(function() {
    const kexuan = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    var Main = {
        data() {
            return {
                cities: kexuan,
                // 存储所有日期的定位数据
                locationData: {},
                // 所有期号和日期
                allQihaoAndDate: [],

                // 所有开奖数据
                allKijiangData: [],
                isReadOnly: true
            };
        },
        created() {
            // 初始化期号数据
            this.initQihaoData();

            // 初始化近三百期数据
            this.getFucai3dListByStartId(300);

            // 查询《历史百期》定位数据
            this.selectDingwei(200);
        },
        methods: {
            // 获取每日定位数据
            getDateData(dateString) {
                let dingweiQihao = this.getQihaoByDate(dateString);
                if (!this.locationData[dingweiQihao]) {
                    this.$set(this.locationData, dingweiQihao, {
                        baiwei: [],
                        shiwei: [],
                        gewei: []
                    });
                }
                return this.locationData[dingweiQihao];
            },

            // 根据日期获取期号信息
            getQihaoByDateAndZhongwen(dateString){
                let qihao = this.getQihaoByDate(dateString);
                return qihao;
            },

            // 根据日期获取定位是否正确
            getDingweiResult(dateString){
                let qihao = this.getQihaoByDate(dateString);
                let qiData = this.locationData[qihao];

                let dingweiResult = "不正确";
                if (qiData){
                    dingweiResult = qiData.dingweiResult;
                }
                return dingweiResult;
            },

            // 根据日期获取期号信息
            getQihaoByDate(dateString){
                let qihao = "";
                this.allQihaoAndDate.forEach(ele =>{
                    if (ele.date == dateString){
                        qihao = ele.period;
                        return;
                    }
                })

                return qihao;
            },

            // 根据日期获取开奖信息
            getKijiang(dateString) {
                let qihao = this.getQihaoByDate(dateString);
                let zj = "";
                this.allKijiangData.forEach(ele =>{
                    if (ele.qihao == qihao){
                        zj = ele.zj;
                        return;
                    }
                })

                return zj;
            },

            // 根据日期获取定位购买金额
            getPurchaseAmount(dateString) {
                let qihao = this.getQihaoByDate(dateString);
                let qiData = this.locationData[qihao];
                console.log("qiData:", qiData)
                let purchaseAmount = '';
                if (qiData != undefined && qiData.purchaseAmount != undefined){
                    purchaseAmount = '￥' + qiData.purchaseAmount;
                }
                return purchaseAmount;
            },

            // 获取近多少期数据
            getFucai3dListByStartId(quantity){
                let _this = this;
                $.ajax({
                    type: "GET",
                    url: "http://localhost:8809/fucai/3d/getFucai3dListByStartId",
                    data: {
                        "quantity": quantity
                    },
                    success: function (response) {
                        console.log("请求成功:", response);
                        _this.allKijiangData = response;
                    }
                });
            },
            // 初始化期号数据
            initQihaoData(){
                this.allQihaoAndDate = publicAllQihaoAndDate;
            },

            // 选号后保存
            handleChecked(dateString, userId){
                let _this = this;
                let dingweiQihao = this.getQihaoByDate(dateString);
                let userName = '朱赤';
                let baiwei = this.locationData[dingweiQihao].baiwei;
                let shiwei = this.locationData[dingweiQihao].shiwei;
                let gewei = this.locationData[dingweiQihao].gewei;

                var data = {
                    dingweiQihao: dingweiQihao,
                    userId: userId,
                    userName: userName,
                    baiwei: JSON.stringify(baiwei),
                    shiwei: JSON.stringify(shiwei),
                    gewei: JSON.stringify(gewei),
                }
                $.ajax({
                    url: 'http://localhost:8809/fucai/3d/dingwei/addOrEditDingwei',
                    type: 'POST',
                    contentType: "application/json", // 指定请求体格式为 JSON
                    data: JSON.stringify(data),
                    success: function(response) {
                        console.log("请求成功:", response);
                        _this.$message({
                            message: '恭喜你,保存成功',
                            type: 'success'
                        });
                    },
                    error: function(xhr, status, error) {
                        console.log('Request failed:', status, error);
                    }
                });
            },

            // 查询某人定位数据
            selectDingwei(userId){
                let _this = this;
                var data = {
                    userId: userId
                }
                $.ajax({
                    type: "POST",
                    url: "http://localhost:8809/fucai/3d/dingwei/selectDingwei",
                    contentType: "application/json", // 指定请求体格式为 JSON
                    data: JSON.stringify(data),
                    success: function (response) {
                        console.log("请求成功:", response);
                        let item = {};
                        for (let i = 0; i < response.length; i++) {
                            _this.$set(item, response[i].dingweiQihao, {
                                baiwei: JSON.parse(response[i].baiwei),
                                shiwei: JSON.parse(response[i].shiwei),
                                gewei: JSON.parse(response[i].gewei),
                                dingweiResult: response[i].dingweiResult,
                                purchaseAmount: response[i].purchaseAmount
                            });

                        }
                        _this.locationData = item;
                    }
                });
            },

            // 判断指定日期是否只读（过去的日期只读）
            isDateReadOnly (dateString) {
                const targetDate = new Date(dateString);
                const today = new Date();
                today.setHours(0, 0, 0, 0); // 忽略时间部分

                return targetDate < today; // 过去的日期为只读
            }
        }
    };

    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')
});